/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cadastramento.java
 *
 * Created on 11/06/2011, 21:48:35
 */
package lp2.projeto;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Raiff
 */
public class Cadastramento extends javax.swing.JFrame {

    private List<String> opinioes= new ArrayList<String>();
    private String caminhoDoArquivo = "teste.data";
    
    /** Creates new form Cadastramento */
    public Cadastramento() {
        initComponents();
        buttonSalvar.setEnabled(false);
        comboBoxEstabelecimentos.setEnabled((false));
    }

    /** This method is called from within the construc]tor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelDeCadastramento = new javax.swing.JPanel();
        comboBoxEstabelecimentos = new javax.swing.JComboBox();
        comboBoxNotas = new javax.swing.JComboBox();
        labelNomeDoUsuario = new javax.swing.JLabel();
        buttonVotar = new javax.swing.JButton();
        labelEstabelecimentos = new javax.swing.JLabel();
        labelNotas = new javax.swing.JLabel();
        textFieldNomeDoUsario = new javax.swing.JTextField();
        buttonSalvar = new javax.swing.JButton();
        buttonSair = new javax.swing.JButton();
        buttonVoltar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pitaqueiro Das 12hs");

        comboBoxEstabelecimentos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bar do Cuscuz e Restaurante", "Baixinho Bar e Restaurante", "Bar do George", "Bar do Santos", "Bodão Bar e Restaurante", "Bonaparte", "Bongustaio", "Cabana do Possidonio", "Cantina da Sayonara", "Cantina de Dona Ines", "Cantina de Olavo", "Cantina do Departamento de Meteorologia", "Cantina do Hall das Placas", "Cantinho Universitário", "Chinatown", "Divino Fogao", "Fazenda", "Giraffas", "Girassol", "Gulas", "Manoel da carne de sol", "Marmitaria ArtCulinaria", "Marmitaria Bom paladar", "Marmitaria da Mama", "Paladar", "Pedro da Picanha", "Qdoca Bar e Restaurante", "Restaurante Brasilia", "Restaurante da Quadra", "Restaurante do Alexandre", "Restaurante do Paraná", "Restaurante Golden in China", "Restaurante Lay China", "Riso", "Spolleto", "Super China Restaurante", "Trailer do Marcus", "Tuxá", "Varandas Bar e Restaurante" }));

        comboBoxNotas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-5: Detesto", "-4: Acho muito ruim", "-3: Acho bastante ruim", "-2: Acho ruim", "-1: Acho um pouco ruim", " 0: Nao conheço", " 1: Nao eh ruim", " 2: Eh bonzinho", " 3: Bastante bom", " 4: Muito bom", " 5: Incrivel" }));
        comboBoxNotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxNotasActionPerformed(evt);
            }
        });

        labelNomeDoUsuario.setText("Nome:");

        buttonVotar.setText("Votar");
        buttonVotar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVotarActionPerformed(evt);
            }
        });

        labelEstabelecimentos.setText("Estabelecimentos:");

        labelNotas.setText("Nota:");

        buttonSalvar.setText("Salvar");
        buttonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSalvarActionPerformed(evt);
            }
        });

        buttonSair.setText("Sair");
        buttonSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSairActionPerformed(evt);
            }
        });

        buttonVoltar.setText("Voltar");
        buttonVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout painelDeCadastramentoLayout = new javax.swing.GroupLayout(painelDeCadastramento);
        painelDeCadastramento.setLayout(painelDeCadastramentoLayout);
        painelDeCadastramentoLayout.setHorizontalGroup(
            painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelDeCadastramentoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelDeCadastramentoLayout.createSequentialGroup()
                        .addComponent(labelNomeDoUsuario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textFieldNomeDoUsario, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(painelDeCadastramentoLayout.createSequentialGroup()
                        .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelEstabelecimentos)
                            .addComponent(labelNotas)
                            .addComponent(buttonSair))
                        .addGap(18, 18, 18)
                        .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(painelDeCadastramentoLayout.createSequentialGroup()
                                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(buttonVoltar)
                                    .addComponent(comboBoxNotas, 0, 178, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(buttonVotar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(buttonSalvar)))
                            .addComponent(comboBoxEstabelecimentos, 0, 247, Short.MAX_VALUE))))
                .addGap(26, 26, 26))
        );
        painelDeCadastramentoLayout.setVerticalGroup(
            painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelDeCadastramentoLayout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNomeDoUsuario)
                    .addComponent(textFieldNomeDoUsario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEstabelecimentos)
                    .addComponent(comboBoxEstabelecimentos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNotas)
                    .addComponent(comboBoxNotas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonVotar))
                .addGap(130, 130, 130)
                .addGroup(painelDeCadastramentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSalvar)
                    .addComponent(buttonSair)
                    .addComponent(buttonVoltar)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelDeCadastramento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelDeCadastramento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-424)/2, (screenSize.height-336)/2, 424, 336);
    }// </editor-fold>//GEN-END:initComponents

    private void comboBoxNotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxNotasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboBoxNotasActionPerformed

    private void buttonVotarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVotarActionPerformed
        opinioes.add(comboBoxNotas.getSelectedItem().toString());
        textFieldNomeDoUsario.setEnabled(false);        
        if(comboBoxEstabelecimentos.getItemCount() <= 1){
            buttonVotar.setEnabled(false);
            comboBoxNotas.setEnabled(false);
            comboBoxEstabelecimentos.setEnabled(false);
            buttonSalvar.setEnabled(true);
        }
        comboBoxEstabelecimentos.removeItemAt(comboBoxEstabelecimentos.getSelectedIndex());
    }//GEN-LAST:event_buttonVotarActionPerformed

    private void buttonSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSairActionPerformed
        System.exit(1);
    }//GEN-LAST:event_buttonSairActionPerformed

    private void buttonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSalvarActionPerformed
        Usuario novoUsuario = new Usuario(textFieldNomeDoUsario.getText(), opinioes, getData());
        System.out.println(novoUsuario.getUsuarioProArquivo());
        try {
            escreveUsuarioNoArquivo(novoUsuario.getUsuarioProArquivo());
        } catch (IOException ex) {
            Logger.getLogger(Cadastramento.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        buttonSalvar.setEnabled(false);
        
        //new cadastrarPanel.setVisible(false); chamar nova frame
    }//GEN-LAST:event_buttonSalvarActionPerformed

    private void buttonVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVoltarActionPerformed
        new MenuInicial().setVisible(true);
        setVisible(false);
    }//GEN-LAST:event_buttonVoltarActionPerformed

    private void escreveUsuarioNoArquivo(String linhaAGravar) throws IOException{
		BufferedWriter out = null;
		String quebraDeLinha = System.getProperty("line.separator"); 
		try {
			out = new BufferedWriter(new FileWriter(caminhoDoArquivo, true));
			out.write(linhaAGravar + quebraDeLinha);
		}catch(IOException erro){
			System.err.println(erro.getMessage());
		}
		finally{
			out.close();
		}
    }
    private String getData(){
        DateFormat date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date dat = new Date();
        return date.format(dat);
    }
    /**
     * @param args the command line arguments
     * @throws FileNotFoundException 
     */
    public static void main(String args[]) throws FileNotFoundException {
        java.awt.EventQueue.invokeLater(new Runnable() {

            
            public void run() {
                new Cadastramento().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSair;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JButton buttonVoltar;
    private javax.swing.JButton buttonVotar;
    private javax.swing.JComboBox comboBoxEstabelecimentos;
    private javax.swing.JComboBox comboBoxNotas;
    private javax.swing.JLabel labelEstabelecimentos;
    private javax.swing.JLabel labelNomeDoUsuario;
    private javax.swing.JLabel labelNotas;
    private javax.swing.JPanel painelDeCadastramento;
    private javax.swing.JTextField textFieldNomeDoUsario;
    // End of variables declaration//GEN-END:variables
}
